home *** CD-ROM | disk | FTP | other *** search
/ Winzipper / Winzipper_ISO.iso / programming / oracle7 7.2 / DB / UTIL72 / SAMPLE6.PC < prev    next >
Encoding:
Text File  |  1995-05-18  |  3.5 KB  |  138 lines

  1. #ifdef RCSID
  2. static char *RCSid = 
  3.    "$Header: sample6.pc.d,v 1.2 93/10/20 12:03:09 kosinski: Exp $ ";
  4. #endif /* RCSID */
  5.  
  6. /* Copyright (c) 1991 by Oracle Corporation */
  7. /*
  8.    NAME
  9.      sample6.pc - <one-line expansion of the name>
  10.    DESCRIPTION
  11.      <short description of component this file declares/defines>
  12.    PUBLIC FUNCTION(S)
  13.      <list of external functions declared/defined - with one-line descriptions>
  14.    PRIVATE FUNCTION(S)
  15.      <list of static functions defined in .c file - with one-line descriptions>
  16.    RETURNS
  17.      <function return values, for .c file with single function>
  18.    NOTES
  19.      <other useful comments, qualifications, etc.>
  20.    MODIFIED   (MM/DD/YY)
  21.     gclossma   12/02/92 -  Creation 
  22. */
  23. /*************************************************************
  24. Sample Program 6:  Calling a Stored Procedure
  25.  
  26. This program connects to ORACLE, declares several host arrays, 
  27. then calls a stored procedure, which fills three PL/SQL table
  28. OUT formal parameters.  The actual parameters are host arrays.
  29. The program calls the procedure repeatedly, getting a batch of
  30. rows and printing the values, until all rows have been retrieved.
  31. *************************************************************/
  32.  
  33. #include <stdio.h>
  34. #include <string.h>
  35.  
  36. typedef char asciz;
  37.  
  38. EXEC SQL BEGIN DECLARE SECTION;
  39.  
  40. /* User-defined type for null-terminated strings */
  41.     EXEC SQL TYPE asciz IS STRING(20);
  42.     asciz     username[20];
  43.     asciz     password[20];
  44.  
  45.     int       dept_no;      /* which department to query? */
  46.     char      emp_name[10][21];
  47.     char      job[10][21];
  48.     float     salary[10];
  49.     int       done_flag;
  50.     int       array_size;
  51.     int       num_ret;      /* number of rows returned */
  52.     int       SQLCODE;
  53. EXEC SQL END DECLARE SECTION;
  54.  
  55. EXEC SQL INCLUDE sqlca;
  56.  
  57. int print_rows();           /* produces program output      */
  58. int sqlerror();             /* handles unrecoverable errors */
  59.  
  60.  
  61. main()
  62. {
  63.     int i;
  64.  
  65.     /* Connect to ORACLE. */
  66.     strcpy(username, "SCOTT");
  67.     strcpy(password, "TIGER");
  68.  
  69.     EXEC SQL WHENEVER SQLERROR DO sqlerror();
  70.  
  71.     EXEC SQL CONNECT :username IDENTIFIED BY :password;
  72.     printf("\nConnected to ORACLE as user: %s\n\n", username);
  73.  
  74.     printf("Enter department number: ");
  75.     scanf("%d", &dept_no);
  76.     fflush(stdin);
  77.  
  78.     /* Print column headers. */
  79.     printf("\n\n");
  80.     printf("%-10.10s%-10.10s%s\n", "Employee", "Job", "Salary");
  81.     printf("%-10.10s%-10.10s%s\n", "--------", "---", "------");
  82.  
  83.     /* Set the array size. */
  84.     array_size = 10;
  85.  
  86.     done_flag = 0;
  87.     num_ret = 0;
  88.  
  89.     /* Array fetch loop - ends when NOT FOUND becomes true. */
  90.     for (;;)
  91.     {
  92.         EXEC SQL EXECUTE 
  93.             BEGIN personnel.get_employees
  94.                 (:dept_no, :array_size, :num_ret, :done_flag,
  95.                  :emp_name, :job, :salary);
  96.             END;
  97.         END-EXEC;
  98.  
  99.         print_rows(num_ret);
  100.  
  101.         if (done_flag)
  102.             break;
  103.     }
  104.  
  105.     /* Disconnect from ORACLE. */
  106.     EXEC SQL COMMIT WORK RELEASE;
  107.     exit(0);
  108. }
  109.  
  110.  
  111. print_rows(n)
  112. int n;
  113. {
  114.     int i;
  115.  
  116.     if (n == 0)
  117.     {
  118.         printf("No rows retrieved.\n");
  119.         return;
  120.     }
  121.  
  122.     for (i = 0; i < n; i++)
  123.         printf("%10.10s%10.10s%6.2f\n",
  124.                emp_name[i], job[i], salary[i]);
  125. }
  126.  
  127.  
  128. sqlerror()
  129. {
  130.     EXEC SQL WHENEVER SQLERROR CONTINUE;
  131.  
  132.     printf("\nORACLE error detected:");
  133.     printf("\n% .70s \n", sqlca.sqlerrm.sqlerrmc);
  134.  
  135.     EXEC SQL ROLLBACK WORK RELEASE;
  136.     exit(1);
  137. }
  138.